﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using SynoManager.Rss;

namespace SynoManager.DownloadStation.Subscriptions
{
    [Serializable()]
    public class SubscriptionItem
    {
        private readonly Subscription subscription;
        private string title;
        private string description;
        private Uri link;
        private string guid;

        private const string _torrentRegex = @"http(s)?://([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?\.torrent";
        private const string _nzbRegex = @"http(s)?://([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?\.nzb";

        private SubscriptionItemState state = SubscriptionItemState.Unknown;
        private DateTime datePublished;

        internal SubscriptionItem(Subscription subscription, RssItem item)
        {
            this.subscription = subscription;
            title = item.Title;
            description = item.Description;
            link = item.Link;
            guid = item.Guid.Name;
            datePublished = item.PubDate;
        }

        public Subscription Subscription
        {
            get { return subscription; }
        }

        public string Title
        {
            get { return title; }
            set { title = value; }
        }

        public string Description
        {
            get { return description; }
            set { description = value; }
        }

        public Uri Link
        {
            get { return link; }
            set { link = value; }
        }

        public string Guid
        {
            get { return guid; }
            set { guid = value; }
        }

        public SubscriptionItemState State
        {
            get { return state; }
            set { state = value; }
        }

        public DateTime DatePublished
        {
            get { return datePublished; }
            set { datePublished = value;}
        }

        public string GetDownloadLink()
        {
            switch (subscription.LinkDiscovery)
            {
                case LinkDiscovery.RssLink:
                    return link.ToString();
                case LinkDiscovery.DescriptionTorrent:
                    return GetDownloadLinkRegEx(_torrentRegex);
                case LinkDiscovery.DescriptionNzb:
                    return GetDownloadLinkRegEx(_nzbRegex);
                case LinkDiscovery.Regex:
                    return GetDownloadLinkRegEx(subscription.CustomLinkDiscoveryRegex);
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }

        private string GetDownloadLinkRegEx(string regularExpression)
        {
            Regex regex = new Regex(regularExpression.Replace("\r", ""),
                                    RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.Compiled);

            // Get the matches
            var matches = regex.Matches(description);

            if (matches.Count != 1)
                return null;

            return matches[0].Value;
        }
    }

    public enum SubscriptionItemState
    {
        Unknown,
        Error,
        Downloaded
    }
}
